package com.zryc.shop.oms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.oms.entity.OrderReturnApply;
import com.zryc.shop.oms.service.IOrderReturnApplyService;
import com.zryc.shop.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 * 订单退货申请 前端控制器
 * </p>
 *
 * @author LiYe
 * @since 2023-02-14
 */
@RestController
@RequestMapping("/oms/returnApply")
@Api(tags = "退货申请管理")
public class OrderReturnApplyController {
    @Autowired
    private IOrderReturnApplyService returnApplyService;

    @GetMapping("/list")
    @ApiOperation("分页查询列表")
    public ResponseResult list(@RequestParam(defaultValue = "1") @ApiParam(value = "页码", required = true) Integer pageNum,
                               @RequestParam(defaultValue = "10") @ApiParam(value = "每页显示条数", required = true) Integer pageSize,
                               @RequestParam(required = false) @ApiParam("服务单号ID") Integer id,
                               @RequestParam(required = false) @ApiParam("退货申请状态") Integer status,
                               @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") @ApiParam("申请时间") Date createTime,
                               @RequestParam(required = false) @ApiParam("处理人员") String handleMan,
                               @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") @ApiParam("处理日期") Date handleTime) {
        Page page = new Page(pageNum, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        if (id != null) {
            queryWrapper.eq("id", id);
        } else {
            if (status != null) {
                queryWrapper.eq("status", status);
            }
            if (!StringUtils.isEmpty(handleMan)) {
                queryWrapper.eq("handle_man", handleMan);
            }
            if (createTime != null) {
                Calendar cal1 = Calendar.getInstance();
                cal1.setTime(createTime);
                queryWrapper.ge("create_time", cal1);//所选日期当前的0点0分0秒 2023-02-14 00:00:00
                Calendar cal2 = Calendar.getInstance();
                cal2.setTime(createTime);
                //cal2.set(Calendar.HOUR, 23); cal2.set(Calendar.MINUTE, 59); cal2.set(Calendar.SECOND, 59);//所选日期当前的0点0分0秒 2023-02-14 23:59:59
                cal2.add(Calendar.DATE, 1);//第二天的0点0分0秒 2023-02-15 00:00:00
                queryWrapper.lt("create_time", cal2);
            }
            if (handleTime != null) {
                Calendar cal1 = Calendar.getInstance();
                cal1.setTime(handleTime);
                queryWrapper.ge("handle_time", cal1);//所选日期当前的0点0分0秒 2023-02-14 00:00:00
                Calendar cal2 = Calendar.getInstance();
                cal2.setTime(handleTime);
                //cal2.set(Calendar.HOUR, 23); cal2.set(Calendar.MINUTE, 59); cal2.set(Calendar.SECOND, 59);//所选日期当前的0点0分0秒 2023-02-14 23:59:59
                cal2.add(Calendar.DATE, 1);//第二天的0点0分0秒 2023-02-15 00:00:00
                queryWrapper.lt("handle_time", cal2);
            }
        }
        returnApplyService.page(page, queryWrapper);
        return new ResponseResult(200, "查询完成", page);
    }

    @GetMapping("/{id}")
    @ApiOperation("查询详情")
    public ResponseResult info(@PathVariable Long id) {
        return new ResponseResult(200, "查询完成", returnApplyService.getById(id));
    }

    @PostMapping("/update/status/{id}")
    @ApiOperation("修改状态")
    public ResponseResult updateStatus(@PathVariable Long id, @RequestBody OrderReturnApply returnApply) {
        returnApply.setId(id);
        if (returnApply.getStatus()==1 || returnApply.getStatus()==3)//状态为1：允许退货 3：拒绝退货
            returnApply.setHandleTime(LocalDateTime.now());
        if (returnApply.getStatus()==2) //退货完成（收到退货）
            returnApply.setReceiveTime(LocalDateTime.now());
        returnApplyService.updateById(returnApply);
        return new ResponseResult(200, "修改成功");
    }

    @PostMapping("/delete")
    @ApiOperation("删除")
    public ResponseResult delete(@RequestParam String ids) {
        returnApplyService.removeBatchByIds(Arrays.asList(ids.split(",")));
        return new ResponseResult(200, "删除成功");
    }
}
